<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>ActiveModel::Validator</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/github.css" type="text/css" media="screen" />
<script src="../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>     
    <div class="banner">
        
            <span>Ruby on Rails v4.0.0</span><br />
        
        <h1>
            <span class="type">Class</span> 
            ActiveModel::Validator 
            
                <span class="parent">&lt; 
                    
                    <a href="../Object.html">Object</a>
                    
                </span>
            
        </h1>
        <ul class="files">
            
            <li><a href="../../files/activemodel/lib/active_model/validator_rb.html">activemodel/lib/active_model/validator.rb</a></li>
            
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
  
    <div class="description">
      
<h2 id="label-Active+Model+Validator">Active Model Validator</h2>

<p>A simple base class that can be used along with <a
href="Validations/ClassMethods.html#method-i-validates_with">ActiveModel::Validations::ClassMethods#validates_with</a></p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span>
  <span class="ruby-identifier">include</span> <span class="ruby-constant">ActiveModel</span><span class="ruby-operator">::</span><span class="ruby-constant">Validations</span>
  <span class="ruby-identifier">validates_with</span> <span class="ruby-constant">MyValidator</span>
<span class="ruby-keyword">end</span>

<span class="ruby-keyword">class</span> <span class="ruby-constant">MyValidator</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveModel</span><span class="ruby-operator">::</span><span class="ruby-constant">Validator</span>
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">validate</span>(<span class="ruby-identifier">record</span>)
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">some_complex_logic</span>
      <span class="ruby-identifier">record</span>.<span class="ruby-identifier">errors</span>[:<span class="ruby-identifier">base</span>] = <span class="ruby-string">&quot;This record is invalid&quot;</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">private</span>
    <span class="ruby-keyword">def</span> <span class="ruby-identifier">some_complex_logic</span>
      <span class="ruby-comment"># ...</span>
    <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Any class that inherits from <a
href="Validator.html">ActiveModel::Validator</a> must implement a method
called <code>validate</code> which accepts a <code>record</code>.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span>
  <span class="ruby-identifier">include</span> <span class="ruby-constant">ActiveModel</span><span class="ruby-operator">::</span><span class="ruby-constant">Validations</span>
  <span class="ruby-identifier">validates_with</span> <span class="ruby-constant">MyValidator</span>
<span class="ruby-keyword">end</span>

<span class="ruby-keyword">class</span> <span class="ruby-constant">MyValidator</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveModel</span><span class="ruby-operator">::</span><span class="ruby-constant">Validator</span>
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">validate</span>(<span class="ruby-identifier">record</span>)
    <span class="ruby-identifier">record</span> <span class="ruby-comment"># =&gt; The person instance being validated</span>
    <span class="ruby-identifier">options</span> <span class="ruby-comment"># =&gt; Any non-standard options passed to validates_with</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>

<p>To cause a validation error, you must add to the <code>record</code>‘s
errors directly from within the validators message.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">MyValidator</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveModel</span><span class="ruby-operator">::</span><span class="ruby-constant">Validator</span>
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">validate</span>(<span class="ruby-identifier">record</span>)
    <span class="ruby-identifier">record</span>.<span class="ruby-identifier">errors</span>.<span class="ruby-identifier">add</span> :<span class="ruby-identifier">base</span>, <span class="ruby-string">&quot;This is some custom error message&quot;</span>
    <span class="ruby-identifier">record</span>.<span class="ruby-identifier">errors</span>.<span class="ruby-identifier">add</span> :<span class="ruby-identifier">first_name</span>, <span class="ruby-string">&quot;This is some complex validation&quot;</span>
    <span class="ruby-comment"># etc...</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>

<p>To add behavior to the initialize method, use the following signature:</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">MyValidator</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveModel</span><span class="ruby-operator">::</span><span class="ruby-constant">Validator</span>
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">options</span>)
    <span class="ruby-keyword">super</span>
    <span class="ruby-ivar">@my_custom_field</span> = <span class="ruby-identifier">options</span>[:<span class="ruby-identifier">field_name</span>] <span class="ruby-operator">||</span> :<span class="ruby-identifier">first_name</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Note that the validator is initialized only once for the whole application
lifecycle, and not on each validation run.</p>

<p>The easiest way to add custom validators for validating individual
attributes is with the convenient <code>ActiveModel::EachValidator</code>.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">TitleValidator</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveModel</span><span class="ruby-operator">::</span><span class="ruby-constant">EachValidator</span>
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">validate_each</span>(<span class="ruby-identifier">record</span>, <span class="ruby-identifier">attribute</span>, <span class="ruby-identifier">value</span>)
    <span class="ruby-identifier">record</span>.<span class="ruby-identifier">errors</span>.<span class="ruby-identifier">add</span> <span class="ruby-identifier">attribute</span>, <span class="ruby-string">'must be Mr., Mrs., or Dr.'</span> <span class="ruby-keyword">unless</span> <span class="ruby-node">%w(Mr. Mrs. Dr.)</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">value</span>)
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>

<p>This can now be used in combination with the <code>validates</code> method
(see <code>ActiveModel::Validations::ClassMethods.validates</code> for more
on this).</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span>
  <span class="ruby-identifier">include</span> <span class="ruby-constant">ActiveModel</span><span class="ruby-operator">::</span><span class="ruby-constant">Validations</span>
  <span class="ruby-identifier">attr_accessor</span> :<span class="ruby-identifier">title</span>

  <span class="ruby-identifier">validates</span> :<span class="ruby-identifier">title</span>, <span class="ruby-identifier">presence</span><span class="ruby-operator">:</span> <span class="ruby-keyword">true</span>
<span class="ruby-keyword">end</span>
</pre>

<p><a href="Validator.html">Validator</a> may also define a <code>setup</code>
instance method which will get called with the class that using that
validator as its argument. This can be useful when there are prerequisites
such as an <code>attr_accessor</code> being present.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">MyValidator</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveModel</span><span class="ruby-operator">::</span><span class="ruby-constant">Validator</span>
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">setup</span>(<span class="ruby-identifier">klass</span>)
    <span class="ruby-identifier">klass</span>.<span class="ruby-identifier">send</span> :<span class="ruby-identifier">attr_accessor</span>, :<span class="ruby-identifier">custom_attribute</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>

<p>This setup method is only called when used with validation macros or the
class level <code>validates_with</code> method.</p>

    </div>
  


  


  
  


  


  
    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
      
        <dt>K</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="Validator.html#method-c-kind">kind</a>,
              </li>
            
              
              <li>
                <a href="Validator.html#method-i-kind">kind</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>N</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="Validator.html#method-c-new">new</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>V</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="Validator.html#method-i-validate">validate</a>
              </li>
            
          </ul>
        </dd>
      
    </dl>
  

  



  

    

    

    


    
      <!-- Section attributes -->
      <div class="sectiontitle">Attributes</div>
      <table border='0' cellpadding='5'>
        
          <tr valign='top'>
            <td class='attr-rw'>
              [R]
            </td>
            <td class='attr-name'>options</td>
            <td class='attr-desc'></td>
          </tr>
        
      </table>
    


    <!-- Methods -->
    
      <div class="sectiontitle">Class Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-c-kind">
            
              <b>kind</b>()
            
            <a href="Validator.html#method-c-kind" name="method-c-kind" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns the kind of the validator.</p>

<pre class="ruby"><span class="ruby-constant">PresenceValidator</span>.<span class="ruby-identifier">kind</span>   <span class="ruby-comment"># =&gt; :presence</span>
<span class="ruby-constant">UniquenessValidator</span>.<span class="ruby-identifier">kind</span> <span class="ruby-comment"># =&gt; :uniqueness</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-c-kind_source')" id="l_method-c-kind_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/53c22e097ad416e32a216fdf0fd70d9a1a47cecf/activemodel/lib/active_model/validator.rb#L104" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-c-kind_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activemodel/lib/active_model/validator.rb, line 104</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">self</span>.<span class="ruby-identifier">kind</span>
  <span class="ruby-ivar">@kind</span> <span class="ruby-operator">||=</span> <span class="ruby-identifier">name</span>.<span class="ruby-identifier">split</span>(<span class="ruby-string">'::'</span>).<span class="ruby-identifier">last</span>.<span class="ruby-identifier">underscore</span>.<span class="ruby-identifier">sub</span>(<span class="ruby-regexp">/_validator$/</span>, <span class="ruby-string">''</span>).<span class="ruby-identifier">to_sym</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">anonymous?</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-c-new">
            
              <b>new</b>(options = {})
            
            <a href="Validator.html#method-c-new" name="method-c-new" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Accepts options that will be made available through the
<code>options</code> reader.</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-c-new_source')" id="l_method-c-new_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/53c22e097ad416e32a216fdf0fd70d9a1a47cecf/activemodel/lib/active_model/validator.rb#L109" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-c-new_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activemodel/lib/active_model/validator.rb, line 109</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">initialize</span>(<span class="ruby-identifier">options</span> = {})
  <span class="ruby-ivar">@options</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">freeze</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
                  
      <div class="sectiontitle">Instance Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-i-kind">
            
              <b>kind</b>()
            
            <a href="Validator.html#method-i-kind" name="method-i-kind" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Return the kind for this validator.</p>

<pre class="ruby"><span class="ruby-constant">PresenceValidator</span>.<span class="ruby-identifier">new</span>.<span class="ruby-identifier">kind</span>   <span class="ruby-comment"># =&gt; :presence</span>
<span class="ruby-constant">UniquenessValidator</span>.<span class="ruby-identifier">new</span>.<span class="ruby-identifier">kind</span> <span class="ruby-comment"># =&gt; :uniqueness</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-kind_source')" id="l_method-i-kind_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/53c22e097ad416e32a216fdf0fd70d9a1a47cecf/activemodel/lib/active_model/validator.rb#L117" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-kind_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activemodel/lib/active_model/validator.rb, line 117</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">kind</span>
  <span class="ruby-keyword">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">kind</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-validate">
            
              <b>validate</b>(record)
            
            <a href="Validator.html#method-i-validate" name="method-i-validate" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Override this method in subclasses with validation logic, adding errors to
the records <code>errors</code> array where necessary.</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-validate_source')" id="l_method-i-validate_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/53c22e097ad416e32a216fdf0fd70d9a1a47cecf/activemodel/lib/active_model/validator.rb#L123" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-validate_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activemodel/lib/active_model/validator.rb, line 123</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">validate</span>(<span class="ruby-identifier">record</span>)
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">NotImplementedError</span>, <span class="ruby-string">&quot;Subclasses must implement a validate(record) method.&quot;</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
                    </div>

    </div>
  </body>
</html>    